import 'dart:ffi';
// import 'package:euromillions/tools/EJ_emsBalls.dart';
import 'package:intl/intl.dart';
// import 'package:euromillions/tools/EJ_config.dart';
// import 'package:euromillions/tools/models/EJ_DataViewModels.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:hexcolor/hexcolor.dart';
// import 'package:kerala_lottery/tools/KaL_drawNumbersView.dart';
// import 'package:kerala_lottery/tools/models/KL_keralaLottoModel.dart';

import '../../Results/EJ_ResultDetailPage.dart';
import '../../Results/KaL_resultDetailPage.dart';
import '../../tools/KaL_Config.dart';
import '../../tools/EJ_ballsView.dart';
import '../../tools/FL_emsBalls.dart';
import '../../tools/KaL_drawNumbersView.dart';
import '../../tools/models/KL_keralaLottoDataModel.dart';
import '../../tools/models/KL_keralaLottoModel.dart';
import '../../tools/models/KaL_DataViewModels.dart';
import '../../tools/models/KaL_LottoModel.dart';

class EJResultView_HomePage extends StatefulWidget {
  EJResultView_HomePage({super.key, this.pageController});
  PageController? pageController;

  @override
  State<EJResultView_HomePage> createState() => _EJResultView_HomePageState();
}

class _EJResultView_HomePageState extends State<EJResultView_HomePage> {
  late KaL_LottoModel _model;
  late KL_keralaLottoModel _keralaLottoModel;
  //
  _goToResultsPage() {
    // NavigationBar
    var Page = widget.pageController!;
    Page.jumpToPage(1);
  }

  ///
  // _goToDetailPage(KaL_LottoModel model) {
  //   Navigator.push(
  //       context,
  //       MaterialPageRoute(
  //           builder: (context) => EJResultDetailPage(
  //                 dataModel: model,
  //               )));
  // }

  void _goToDetailPage(List prizes) {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => KalResultdetailpage(
                  prizes: prizes,
                )));
  }

  ///initdata data
  void initData() {
    List allDataModels = KL_keralaLottoDataModel().allDataModels;
    var model = allDataModels[0];
    _keralaLottoModel = model;

    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    initData();
  }

  @override
  Widget build(BuildContext context) {
    ///
    var drawCode = _keralaLottoModel.draw_code;
    var gameName = _keralaLottoModel.game_name;
    var lottoName = '$gameName($drawCode)';

    DateTime drawDateTimeUTC =
        DateTime.parse(_keralaLottoModel.draw_datetime_utc);
    var drawDateTime = DateFormat('EEE, MMM d, y').format(drawDateTimeUTC);

    var numbersMap = {};
    List balls = [];
    PrizeModel prizeModel = _keralaLottoModel.prizes![0];
    List ticketNumbers = prizeModel.ticket_numbers!;
    if (ticketNumbers.isNotEmpty) {
      String numberStr = ticketNumbers[0];
      // // 使用 split 方法将字符串转换为字符串数组
      List<String> charArray = numberStr.split('');
      // numbersMap = charArray;
      for (var i = 0; i < charArray.length; i++) {
        var ball = {
          'number': charArray[i],
          'isRight': false,
          'isNormal': true
        }; //isNormal代表是否对比中奖号码，true不用对比，false需要对比。isRight代表是否中奖号码
        balls.add(ball);
      }
      var title = prizeModel.rank_title + ' Prize';
      numbersMap = {'title': title, 'balls': balls};
    }

    return Container(
      // height: 100,
      margin: const EdgeInsets.all(10),
      padding: const EdgeInsets.only(top: 10),
      decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.2), // 阴影颜色
              spreadRadius: 1, // 扩散半径
              blurRadius: 10, // 模糊半径
              offset: const Offset(0, 1), // 阴影偏移
            ),
          ],
          color: Colors.white,
          borderRadius: const BorderRadius.all(Radius.circular(10))),
      child: Column(
        children: [
          Container(
            height: 25,
            // alignment: Alignment.center,
            padding:
                const EdgeInsets.only(left: 22, top: 4, right: 25, bottom: 4),
            // margin: EdgeInsets.only(left: 15, right: 15),
            decoration: const BoxDecoration(
                // color: KaL_Config.AL_MainColor.withOpacity(0.8),
                color: Colors.black,
                borderRadius: BorderRadius.all(Radius.circular(25 * 0.5))),
            child: const Text(
              'WINNING NUMBERS',
              style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.w500,
                  fontSize: 12),
            ),
          ),
          const SizedBox(height: 15),
          Text(lottoName,
              style: TextStyle(
                fontWeight: FontWeight.w900,
                fontSize: 17,
                color: HexColor('#D32F7D'),
              )),
          const SizedBox(height: 10),
          Text(
            drawDateTime,
            style: const TextStyle(
              fontWeight: FontWeight.w400,
              fontSize: 16,
              color: Colors.black,
            ),
          ),
          const SizedBox(height: 10),
          // drawingNumbersView(),
          KalDrawnumbersview(data: numbersMap),
          const SizedBox(height: 15),
          viewDetailBtn(),
          const SizedBox(height: 10),
          historyResultBtn(),
          const SizedBox(height: 15),
        ],
      ),
    );
  }

  Widget historyResultBtn() {
    double btnH = 40;
    return GestureDetector(
      onTap: () {
        debugPrint('_goToResultsPage()');
        _goToResultsPage();
      },
      child: Container(
        height: btnH,
        width: 300,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            // color: HexColor('##222529'),
            borderRadius: BorderRadius.all(Radius.circular(btnH * 0.5))),
        child: Text(
          'PREVIOUS RESULTS',
          style: TextStyle(
            fontSize: 15,
            fontWeight: FontWeight.w600,
            color: KaL_Config.KaL_MainColor,
          ),
        ),
      ),
    );
  }

  Widget viewDetailBtn() {
    double btnH = 40;
    return GestureDetector(
      onTap: () {
        debugPrint('_gotoHistoryPage()');
        _goToDetailPage(_keralaLottoModel.prizes!);
      },
      child: Container(
        height: btnH,
        width: 300,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: KaL_Config.KaL_MainColor,
            borderRadius: BorderRadius.all(Radius.circular(btnH * 0.5))),
        child: const Text(
          'VIEW FULL DETAILS',
          style: TextStyle(
              fontSize: 15, fontWeight: FontWeight.w600, color: Colors.white),
        ),
      ),
    );
  }

  Widget ballsView() {
    List<Widget> balls = [];
    for (var i = 0; i < 6; i++) {
      balls.add(regularBall());
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: balls,
    );
  }

  Widget regularBall() {
    double WH = 40;
    return Container(
      color: Colors.white,
      width: WH,
      height: WH,
      padding: const EdgeInsets.all(5),
      child: Container(
        decoration: BoxDecoration(
            color: Colors.blue,
            borderRadius: BorderRadius.all(Radius.circular(WH * 0.5))),
        child: const Center(
          child: Text(
            '9',
            style: TextStyle(
                fontWeight: FontWeight.w600, fontSize: 15, color: Colors.white),
          ),
        ),
      ),
    );
  }
}
